/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.Dao;

import Controler.CXuLy;
import Model.Pojo.Parameters;
import Model.Pojo.Sanpham;
import Model.Util.HibernateUtil;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;

/**
 *
 * @author THANH QUAN
 */
public class DAOSanPham
{

    /**
     * Them san pham
     *
     * @param dtoSP
     * @throws Exception
     */
    public void InsertSanPham(Sanpham dtoSP) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            //Kiem tra san pham co trong database hay khong?

            //Insert vao database neu chua co san pham
            DAOProvider.Insert(session, dtoSP);

        }
        catch (Exception ex)
        {
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
    }

    /**
     * update thong tin san pham
     *
     * @param dtoSP
     * @throws Exception
     */
    public void UpdateSanPham(Sanpham dtoSP) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            //Kiem tra san pham co trong database hay khong?

            //Insert vao database neu chua co san pham
            DAOProvider.Update(session, dtoSP);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
    }

    /**
     * Lay danh sach san pham
     *
     * @param tinhtrang
     * @return
     */
    public List LayDanhSachSanPham(boolean tinhtrang) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();

        String hql = "from Sanpham sp where sp.tinhtrang =:tinhtrang";
        List list = null;
        try
        {
            list = DAOProvider.Select(session, hql, new Parameters[]
            {
                new Parameters("tinhtrang", tinhtrang)
            });
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
        return list;
    }

    /**
     * Chi tiet san pham
     *
     * @param id_khachhang
     * @return
     */
    public Sanpham ChiTieSanPham(int id_sanpham) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();

        String hql = "from Sanpham kh where kh.id =:id_sanpham";
        List query = null;
        try
        {
            query = DAOProvider.Select(session, hql, new Parameters[]
            {
                new Parameters("id_sanpham", id_sanpham)
            });
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
        return query != null ? (Sanpham) query.get(0) : null;

    }

    /**
     * Xoa san pham
     */
    public void XoaSanPham(int id_sanpham) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();



        try
        {
            DAOProvider.Delete(session, DAOProvider.
                    SelectOne(session, Sanpham.class, id_sanpham));
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
    }

    /**
     * Tìm kiếm sản phẩm theo 3 tiêu chí là: Mã sản phẩm, tên sản phẩm, ngày kết thúc bán hàng
     *
     * @param masanpham
     * @param tensanpham
     * @param date
     * @return
     */
    public List TimKiemSanPham(String masanpham, String tensanpham, String[] date, int danhmuc)
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        List list = null;
        String hql = "";

        if (date.length != 2)
        {
            if (masanpham != "")
            {
                hql = "from Sanpham sp inner join fetch sp.danhmucsanpham dm ";
                hql += "where dm.id=:danhmuc and (sp.masp=:masanpham)";
                try
                {
                    list = DAOProvider.Select(session, hql, new Parameters[]
                    {
                        new Parameters("danhmuc", danhmuc), new Parameters("masanpham",
                                                                           masanpham)
                    });


                }
                catch (Exception ex)
                {
                    Logger.getLogger(DAOSanPham.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
                finally
                {
                    session.close();
                }
            }
            else
            {
                hql = "from Sanpham sp inner join fetch sp.danhmucsanpham dm ";
                hql += "where dm.id=:danhmuc and (sp.tensp like :tensanpham)";
                try
                {
                    list = DAOProvider.Select(session, hql, new Parameters[]
                    {
                        new Parameters("danhmuc", danhmuc), new Parameters("tensanpham",
                                                                           "%" + tensanpham + "%")
                    });


                }
                catch (Exception ex)
                {
                    Logger.getLogger(DAOSanPham.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
                finally
                {
                    session.close();
                }
            }
        }
        else
        {
            if (masanpham != "")
            {
                hql = "from Sanpham sp inner join fetch sp.danhmucsanpham dm ";
                hql += "where dm.id=:danhmuc and sp.masp=:masanpham and sp.ngayketthuc between :date1 and :date2";
                try
                {
                    list = DAOProvider.Select(session, hql, new Parameters[]
                    {
                        new Parameters("danhmuc", danhmuc), new Parameters("masanpham",
                                                                           masanpham),
                        new Parameters("date1",
                                       CXuLy.ConvertStringToDate(date[0])), new Parameters("date2",
                                                                                           CXuLy.
                        ConvertStringToDate(date[1]))
                    });


                }
                catch (ParseException ex)
                {
                    Logger.getLogger(DAOSanPham.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
                catch (Exception ex)
                {
                    Logger.getLogger(DAOSanPham.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
                finally
                {
                    session.close();
                }
            }
            else
            {
                hql = "from Sanpham sp inner join fetch sp.danhmucsanpham dm ";
                hql += "where dm.id=:danhmuc and sp.tensp like :tensanpham and sp.ngayketthuc between :date1 and :date2";
                try
                {
                    list = DAOProvider.Select(session, hql, new Parameters[]
                    {
                        new Parameters("danhmuc", danhmuc), new Parameters("tensanpham",
                                                                           "%" + tensanpham + "%"),
                        new Parameters("date1",
                                       CXuLy.ConvertStringToDate(date[0])), new Parameters("date2",
                                                                                           CXuLy.
                        ConvertStringToDate(date[1]))
                    });


                }
                catch (ParseException ex)
                {
                    Logger.getLogger(DAOSanPham.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
                catch (Exception ex)
                {
                    Logger.getLogger(DAOSanPham.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
                finally
                {
                    session.close();
                }
            }
        }
        return list;
    }
}
